package com.lantosec.hmrp.modules.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.kit.tree.TreeNodeVo;
import com.lantosec.hmrp.core.mvc.BaseController;
import com.lantosec.hmrp.modules.system.entity.SysArea;
import com.lantosec.hmrp.modules.system.service.ISysAreaService;
import com.lantosec.hmrp.modules.system.vo.SysAreaVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 *
 * 区域表 控制器
 *
 * @author AutoCode
 * @since 2020-02-24
 */
@Api(value = "区域表控制器", tags = "区域表控制器")
@RestController
@RequestMapping("/area")
@Log4j2
public class SysAreaController extends BaseController {
    @Autowired
    private ISysAreaService service;

    @GetMapping("all")
    @ApiOperation(value = "查询全部")
    public Result<List<SysAreaVO>> all(){
        return Result.ok(entity2VO(service.list()));
    }

    @GetMapping("page")
    @ApiOperation(value = "分页查询")
    public Result<List<SysAreaVO>> page(@ApiParam(value = "id") @RequestParam(name = "id",required = false) String id,
                                        @ApiParam(value = "区域名称") @RequestParam(name = "areaName",required = false) String areaName){
        List<SysArea> sysAreaS = service.list( new QueryWrapper<SysArea>()
                        .eq(StringUtils.isNotEmpty(id), String.valueOf(SysArea.ColName.parent_id),id)
                        .like(areaName != null,SysArea.ColName.area_name.toString(),areaName)
                        .orderByAsc("area_order"));
        return Result.ok(entity2VO(sysAreaS));
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "主键查询")
    public Result<SysAreaVO> get(@ApiParam(value = "ID", required = true) @PathVariable(name="id") String id){
        return Result.ok(entity2VO(service.getById(id)));
    }

    @PostMapping("create")
    @ApiOperation(value = "新增数据")
    public Result<String> create(@ApiParam(value = "区域表VO对象", required = true) @RequestBody @Validated SysAreaVO vo, BindingResult bindingResult){
        validationHandler(bindingResult);
        SysArea entity = vo.convertToEntity();
        service.create(entity);
        return Result.ok(entity.getAreaId());
    }

    @PutMapping("update")
    @ApiOperation(value = "修改数据")
    public Result<Void> update(@ApiParam(value = "区域表VO对象", required = true) @RequestBody SysAreaVO vo){
        SysArea entity = vo.convertToEntity();
        service.updateById(entity);
        return Result.ok();
    }

    @DeleteMapping(value = "delete/{ids}")
    @ApiOperation("逻辑删除")
    public Result<Void> delete(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name="ids") String ids){
        if(StringUtils.isNotEmpty(ids)){
            service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return Result.ok();
    }

    @GetMapping("getAreaTree")
    @ApiOperation("查询区域树")
    public Result<List<TreeNodeVo>> getAreaTree() {
        return Result.ok(service.getAreaTree());
    }

    @GetMapping("getUserDeptTree")
    @ApiOperation("查询部门树带负责人信息")
    public Result<List<TreeNodeVo>> getUserDeptTree(){
        return Result.ok(service.getUserDeptTree());
    }

    @PutMapping("updateOrder")
    @ApiOperation(value = "更新区域排序")
    public Result<Void> updateOrder(@ApiParam(value = "区域ID", required = true) @RequestParam(name = "id") String id,
                                    @ApiParam(value = "排序值", required = true) @RequestParam(name = "areaOrder") Integer areaOrder) {
        service.updateOrder(id,areaOrder);
        return Result.ok();
    }
}
